const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const router = express.Router();


app.use(bodyParser());

router.get('/hello', (req, res, next) => {
	res.send('<form methods="post" action="/api"><input type="text" /><button type="submit">提交</button></form>');
})

router.get('/hello/:name', (req, res, next) => {
	let name = req.params.name;
	res.send(`Hello, ${name}!`);
})

router.post('/api', async (req, res, next) => {
	try{
		await next();
		console.log(req.body,res.json(req.body));
	}catch(e){
		console.log(e)
	}
})

app.use((req, res, next) => {
	console.log(`Time: ${Date.now()}`)
	next();
})



app.use('/',router)

app.listen(8080, ()=>{
	console.log('server is running at 8080 port:')
})